﻿app.controller('CouponCtrl',
    ['$scope', '$http', '$filter', 'httpPlus', 'utils',
    function ($scope, $http, $filter, httpPlus, utils) {
        utils.setTitle('我的优惠券');
        $scope.tabs = [
        {
            "Name": "未使用",
            "Value": "Enable"
        },
         {
             "Name": "已过期",
             "Value": "TimeOut"
         },
          {
              "Name": "已使用",
              "Value": "Used"
          }
        ];
        $scope.currTab = $scope.tabs[0];
        $scope.couponsList = [];

        var couponPageMgr = {
            pageNo: 1,
            pageSize: Math.floor((window.screen.height - 50 - 39) / 90),
            total: 0
        };

        $scope.btnCurrTab_click = function (data) {
                $scope.couponsList = [];
                $scope.currTab = data;
                couponPageMgr.pageNo = 1;
                refreshCoupon($scope.currTab.Value, couponPageMgr.pageNo, couponPageMgr.pageSize);
            };
            $scope.showMore_click = function () {
                if ($scope.canShowMore()) {
                    couponPageMgr.pageNo++;
                refreshCoupon($scope.currTab.Value, couponPageMgr.pageNo, couponPageMgr.pageSize);
            }
        };

        $scope.canShowMore = function () {
            return !(couponPageMgr.pageNo * couponPageMgr.pageSize >= couponPageMgr.total) && couponPageMgr.total > 0;
        };

        var refreshCoupon = function (_status, _pageNo, _pageSize) {
            httpPlus
                .MMB2RRRApi('MyCouponBLL/InitMyCouponPage', {
                    _couponStatus: _status,
                    _pageNo: _pageNo,
                    _pageSize: _pageSize,
                },'wechat')
                .then(function (data) {
                    console.log(data);
                    couponPageMgr.total = data.DataTotal;
                    data.CouponsList.forEach(function (val) {
                        $scope.couponsList.push({
                            couponCode: val.CouponCode,
                            couponType: val.CouponType,
                            couponId: val.CouponID,
                            couponName: val.CouponName,
                            endTime: val.EndTime.substring(0, 10),
                            startTime: val.StartTime.substring(0, 10)
                        })
                    });
                }, function (error) {
                    alert(error);
                });
        };

        refreshCoupon($scope.currTab.Value, couponPageMgr.pageNo, couponPageMgr.pageSize);


    }]);